require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')
require('../lib/iconfont/iconfont.css')

require('../css/register.less')

// 验证码
const CaptchaMini=require('captcha-mini')

// 确认验证码是否正确
const axios=require('axios')

// weui-轻提示
require('weui')
const weui= require('weui.js')

let tel=document.querySelector('#tel')
let criptcha=document.querySelector('#criptcha')
let password=document.querySelector('#password')
let againPwd=document.querySelector('#again-pwd')
let registerBtn=document.querySelector('#registerBtn')
let errMsg=document.querySelector('.err-msg')

window.addEventListener('DOMContentLoaded',function(){
    // 插件验证码
    let captchaCode = ''; // 当前验证码

    // 创建验证码渲染器
    const captchaDrawer = new CaptchaMini({
        lineWidth: 1,   //线条宽度
        lineNum: 4,       //线条数量
        dotR: 2,          //点的半径
        dotNum: 10,       //点的数量
        preGroundColor: [10, 80],    //前景色区间
        backGroundColor: [150, 250], //背景色区间
        fontSize: 30,           //字体大小
        fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'],  //字体类型
        fontStyle: 'fill',      //字体绘制方法，有fill和stroke
        content: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN',  //验证码内容
        length: 4    //验证码长度
    });
    captchaDrawer.draw(document.querySelector('.code'), function(r) {
        console.log(r);
        captchaCode=r
    });
    
    // 点击按钮，成功就跳转
    registerBtn.addEventListener('click',function(){
        let telVal=tel.value
        let criptchaVal=criptcha.value
        let passwordVal=password.value
        let againPwdVal=againPwd.value

        if(!/^1[3-9][0-9]{9}$/.test(telVal)){
            errMsg.textContent='手机号输入错误'
            return 
        }else if(captchaCode.toLowerCase() !=criptchaVal.toLowerCase()){
            errMsg.textContent='验证码输入错误'
            return
        }else if(!/^[a-zA-Z]\w{5,11}$/.test(passwordVal)){
            errMsg.textContent='密码为6~12位'
            return
        }else if(againPwdVal !== passwordVal){
            errMsg.textContent='两次输入密码不一致'
            return
        }else{
            errMsg.textContent=''
            tel.value=criptcha.value=password.value=againPwd.value=''

            // 注册账号
            axios.post('http://139.9.177.51:8099/users/add',{
                account: telVal,
                password: passwordVal
            }).then(function(res){
                if(res.data.status===0){
                    // 弹出轻提示：成功
                    weui.toast('注册成功', {
                        duration: 1000,
                        callback: function(){ 
                            location.href='./login.html'
                         }
                    });
                }else{
                    // 弹出轻提示：失败
                    weui.alert(res.data.msg)
                }
            })

        }
    
})
})




